package demo03;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: WangPeng
 * @createTime: 2022/10/31 17:35
 * @description:
 * @version: v1.0
 */
public class MyTicket implements Runnable{

    private int ticket = 100;
    private final static Object LOCK = new Object();
    //创建锁对象
    Lock lock = new ReentrantLock();
    //思考:多个线程 都是对这100票进行操作。
    @Override
    public void run() {

        while (true) {
//            synchronized (LOCK) {
            try {
                lock.lock();
                if (ticket > 0) {
                    System.out.println(Thread.currentThread().getName() + "卖了一张票,剩余：" + --ticket);
                } else {
                    System.out.println("票卖完了");
                    break;
                }
            }finally {
                lock.unlock();
            }
//            }
        }
    }
}
